package utils;

import java.util.HashSet;

import models.city.City;
import models.game.*;
import models.players.*;
import models.cards.*;

public abstract class PlayerUtils {

	public static void drive(Player player, Game game, String city) {
		player.setLocation(game.getCities().get(city));
	}

	public static void directFlight(Player player, Game game, String city) {
		player.setLocation(game.getCities().get(city));
		IPlayerCard card = player.getHand().get(city);
		player.removeCardFromHand(card);
		game.getPlayerDeck().addToDiscardPile(player.getHand().get(player.getLocation().getName()));
	}

	public static void charterFlight(Player player, Game game, String city) {
		IPlayerCard card = player.getHand().get(player.getLocation().getName());
		player.removeCardFromHand(card);
		game.getPlayerDeck().addToDiscardPile(player.getHand().get(player.getLocation().getName()));
		player.setLocation(game.getCities().get(city));
	}

	public static void shuttleFlight(Player player, Game game, String city) {
		player.setLocation(game.getCities().get(city));
	}

	public static void buildResearchStation(Player player, Game game) {
		player.getLocation().setHasResearchCentre(true);
		IPlayerCard card = player.getHand().get(player.getLocation().getName());
		player.removeCardFromHand(card);
		game.getPlayerDeck().addToDiscardPile(card);
	}

	public static void treatDisease(Player player, Game game) {
		City city = player.getLocation();
		if (city.getDisease().getIsCured()) {
			while (city.getInfectionLevel() > 0) {
				city.decreaseInfectionLevel();
				city.getDisease().increaseInfectionsLeft();
			}
		} else {
			city.decreaseInfectionLevel();
			city.getDisease().increaseInfectionsLeft();
		}
	}

	public static void discoverACure(Player player, Game game, String disease) {
		game.getDiseases().get(disease).setCured(true);
		int i = 5;
		HashSet<IPlayerCard> forDiscarding = new HashSet<IPlayerCard>();
		for (IPlayerCard card : player.getHand().values()) {
			if (card instanceof PlayerCityCard) {
				i--;
				forDiscarding.add(card);
			}
			if (i == 0) {
				for (IPlayerCard discardCard : forDiscarding) {
					player.removeCardFromHand(discardCard);
					game.getPlayerDeck().addToDiscardPile(discardCard);
				}
				break;
			}
		}
		
	}

	public static void recieveKnowledge(Player player, Game game, String card) {
		for (Player otherPlayer : PlayerValidationAndOptions.playersWithSameLocation(player, game)) {
			if (otherPlayer.getHand().containsKey(card)) {
				IPlayerCard tradeCard = otherPlayer.getHand().get(card);
				otherPlayer.removeCardFromHand(tradeCard);
				player.putCardInHand(tradeCard);
			}
		}
	}

	public static void shareKnowledge(Player player, Game game, String otherPlayerName) {
		IPlayerCard tradeCard = player.getHand().get(player.getLocation().getName());
		for (Player otherPlayer : PlayerValidationAndOptions.playersWithSameLocation(player, game)) {
			if (otherPlayer.getUsername().equals(otherPlayerName)) {
				otherPlayer.putCardInHand(tradeCard);
				player.removeCardFromHand(tradeCard);					
				break;
			}
		}
	}
	
}
